//障碍自身控制脚本
cc.Class({
    extends: cc.Component,

    properties: {
        //最大横向位置
        Max_X: 0,
        //最小横向位置
        Min_X: 0,
        //最大纵向位置
        Max_Y: 0,
        //最小纵向位置
        Min_Y: 0,
        //障碍子节点
        Pipe_Child: {
            default: [],
            type: cc.Node
        }
    },


    onLoad () {
        this.Pe_Speed = Game.Self.Pe_Speed;
        this.Init_Pipe(this.Pipe_Child[0]);
        this.Init_Pipe(this.Pipe_Child[1]);
    },

    update (dt) {
        if(Game.End == true) {
            return;
        }
        this.Scroll_Pipe(this.Pe_Speed,dt);
    },

    //初始化障碍
    Init_Pipe(node) {
        var ver_y = this.Create_Random(this.Max_Y,this.Min_Y);
        node.y = ver_y;
        this.Setting_RandomY(node,ver_y);
    },

    //障碍滚动方法
    Scroll_Pipe (speed,int) {
        var len = this.Pipe_Child.length;
        for(var i = 0; i < len; i++) {
            if(this.Pipe_Child[i].x - (speed * int) <= this.Max_X) {
                this.Pipe_Child[i].x = this.Min_X;
                this.Init_Pipe(this.Pipe_Child[i]);
            }
            this.Pipe_Child[i].x -= speed * int;
        }
    },

    //生成某范围内的随机数
    Create_Random(max,min) {
        var random;
        var seed_one = Math.random();
        var seed_two = Math.random();
        var center = parseInt(Math.random() * 100);
        var random_max = parseInt(seed_one * max);
        var random_min = parseInt(seed_two * min);
        if(center % 2 == 0) {
            random = random_max;
        }else {
            random = random_min;
        }
        return random;
    },

    //将当前随机的Y轴偏移量赋值给子节点下的脚本
    Setting_RandomY (child,randomy) {
        var double_child = null;
        double_child = child.children;
        for(var i = 0; i < double_child.length - 1; i++) {
            double_child[i + 1].getComponent("PipeSelf").Random_Y = randomy;
        }
    },

    //重置障碍位置
    Reset_Position () {
        this.Pipe_Child[0].x = 188;
        this.Pipe_Child[1].x = 372;
    }
});
